// drawing-tools.js
// 绘制工具功能
class DrawingTools {
    constructor(drawCanvas, previewCanvas, screenshotCore) {
        this.drawCanvas = drawCanvas;
        this.previewCanvas = previewCanvas;
        // 设置willReadFrequently为true以提高性能
        this.screenshotCore = screenshotCore;
        this.isDrawing = false;
        this.lastX = 0;
        this.lastY = 0;
        this.startX = 0;
        this.startY = 0;
        this.currentTool = 'draw';
        this.currentColor = '#e74c3c';
        this.currentSize = 16;
    }

    // 设置当前工具
    setCurrentTool(tool) {
        this.currentTool = tool;
    }

    // 设置当前颜色
    setCurrentColor(color) {
        this.currentColor = color;
    }

    // 设置当前大小
    setCurrentSize(size) {
        this.currentSize = parseInt(size);
    }

    // 获取正确的鼠标坐标
    getMousePos(event) {
        const rect = this.drawCanvas.getBoundingClientRect();
        const scale = this.screenshotCore.calculateScale();
        return {
            x: (event.clientX - rect.left) * scale.scaleX,
            y: (event.clientY - rect.top) * scale.scaleY
        };
    }

    // 绘制箭头
    drawArrow(fromX, fromY, toX, toY, context) {
        const headlen = 15 * this.currentSize;
        const angle = Math.atan2(toY - fromY, toX - fromX);

        context.beginPath();
        context.moveTo(fromX, fromY);
        context.lineTo(toX, toY);
        context.lineTo(toX - headlen * Math.cos(angle - Math.PI / 6), toY - headlen * Math.sin(angle - Math.PI / 6));
        context.moveTo(toX, toY);
        context.lineTo(toX - headlen * Math.cos(angle + Math.PI / 6), toY - headlen * Math.sin(angle + Math.PI / 6));
        context.strokeStyle = this.currentColor;
        context.lineWidth = this.currentSize;
        context.stroke();
    }

    // 开始绘制
    startDrawing(e) {
        if (!canvasWrapper.style.display || canvasWrapper.style.display === 'none') return;

        this.isDrawing = true;
        const pos = this.getMousePos(e);
        this.lastX = pos.x;
        this.lastY = pos.y;
        this.startX = pos.x;
        this.startY = pos.y;

        // 对于自由绘制，开始一条新路径
        if (this.currentTool === 'draw') {
            drawCtx.beginPath();
            drawCtx.moveTo(this.lastX, this.lastY);
        }
    }

    // 绘制中
    draw(event) {
        if (!this.isDrawing) return;

        const pos = this.getMousePos(event);

        if (this.currentTool === 'draw') {
            // 自由绘制 - 连续绘制路径
            drawCtx.lineTo(pos.x, pos.y);
            drawCtx.strokeStyle = this.currentColor;
            drawCtx.lineWidth = this.currentSize;
            drawCtx.lineCap = 'round';
            drawCtx.lineJoin = 'round';
            drawCtx.stroke();
        }
        else {
            // 清除预览画布
            previewCtx.clearRect(0, 0, this.previewCanvas.width, this.previewCanvas.height);
            // 方案1
            switch (this.currentTool) {
                case 'rect':
                    const width = pos.x - this.startX;
                    const height = pos.y - this.startY;

                    previewCtx.strokeStyle = this.currentColor;
                    previewCtx.lineWidth = this.currentSize;
                    previewCtx.strokeRect(this.startX, this.startY, width, height);
                    break;

                case 'line':
                    previewCtx.beginPath();
                    previewCtx.moveTo(this.startX, this.startY);
                    previewCtx.lineTo(pos.x, pos.y);
                    previewCtx.strokeStyle = this.currentColor;
                    previewCtx.lineWidth = this.currentSize;
                    previewCtx.stroke();
                    break;

                case 'circle':
                    const radiusX = Math.abs(pos.x - this.startX) / 2;
                    const radiusY = Math.abs(pos.y - this.startY) / 2;
                    const centerX = this.startX + (pos.x - this.startX) / 2;
                    const centerY = this.startY + (pos.y - this.startY) / 2;

                    previewCtx.beginPath();
                    previewCtx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
                    previewCtx.strokeStyle = this.currentColor;
                    previewCtx.lineWidth = this.currentSize;
                    previewCtx.stroke();
                    break;
                case 'arrow':
                    this.drawArrow(this.startX, this.startY, pos.x, pos.y, previewCtx);
                    break;

                default: break;
            }
        }
    }

    // 结束绘制
    stopDrawing(e) {
        if (!this.isDrawing) return false;

        const pos = this.getMousePos(e);
        let didDraw = false;

        if (this.currentTool === 'text') {
            const text = prompt('请输入要添加的文本:', '示例文本');
            if (text) {
                // 使用更大的字体
                const fontSize = 20 + (this.currentSize * 4);
                drawCtx.font = `bold ${fontSize}px Arial`;
                drawCtx.fillStyle = this.currentColor;
                drawCtx.fillText(text, pos.x, pos.y);
                didDraw = true;
            }
        }
        else if (this.currentTool === 'draw') {
            // 自由绘制已经实时绘制
            didDraw = true;
        }
        else {
            // 将预览画布内容绘制到主画布
            drawCtx.drawImage(this.previewCanvas, 0, 0);

            // 清除预览画布
            previewCtx.clearRect(0, 0, this.previewCanvas.width, this.previewCanvas.height);

            didDraw = true;
        }

        this.isDrawing = false;
        return didDraw;
    }

    // 调整画布大小
    resizeCanvases(width, height) {
        this.drawCanvas.width = width;
        this.drawCanvas.height = height;
        this.previewCanvas.width = width;
        this.previewCanvas.height = height;
    }
}